package com.jz.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jz.mapper.ModuleTCollectionMapper;
import com.jz.mapper.ModuleTMessageMapper;
import com.jz.mapper.auto.TMessageMapper;
import com.jz.mapper.auto.TShopMapper;
import com.jz.mapper.auto.TStudentMapper;
import com.jz.module.dto.TMessageDto;
import com.jz.module.dto.base.PageInfoDto;
import com.jz.module.entity.TMessage;
import com.jz.module.entity.TShop;
import com.jz.module.entity.TStudent;
import com.jz.service.MessageService;
import com.jz.util.ResultUtil;
import com.jz.util.SysCode;
import com.jz.util.UUIDUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    @SuppressWarnings("all")
    private TMessageMapper tMessageMapper;

    @Autowired
    @SuppressWarnings("all")
    private ModuleTMessageMapper moduleTMessageMapper;

    @Autowired
    @SuppressWarnings("all")
    private TStudentMapper tStudentMapper;

    @Autowired
    @SuppressWarnings("all")
    private TShopMapper tShopMapper;

    @Autowired
    @SuppressWarnings("all")
    private ModuleTCollectionMapper moduleTCollectionMapper;

    @Override
    public ResultUtil page(TMessageDto dto) {
        PageHelper.startPage(dto.getPageNo(), dto.getPageSize());
        List<TMessageDto> tMessages = moduleTMessageMapper.listBySchool(dto);
        PageInfo<TMessageDto> info = new PageInfo<>(tMessages);
        PageInfoDto<TMessageDto> result = new PageInfoDto<TMessageDto>();
        BeanUtils.copyProperties(info, result);
        return ResultUtil.success(result);
    }

    @Override
    public ResultUtil listByParam(TMessageDto dto) {
        List<TMessageDto> tMessages = moduleTMessageMapper.listBySchool(dto);
        return ResultUtil.success(tMessages);
    }

    @Override
    public ResultUtil query(TMessageDto dto) {
        if (dto == null || StringUtils.isBlank(dto.getId())) {
            return ResultUtil.paramError();
        }
        TMessage tMessage = tMessageMapper.selectByPrimaryKey(dto.getId());
        if (tMessage == null) {
            return ResultUtil.error(SysCode.NO_DATA);
        }
        TMessageDto result = new TMessageDto();
        BeanUtils.copyProperties(tMessage, result);
        return ResultUtil.success(result);
    }

    @Override
    public ResultUtil save(TMessageDto dto) {
        if (dto == null) {
            return ResultUtil.paramError();
        }
        if (StringUtils.isBlank(dto.getId())) {
            TMessage insert = new TMessage();
            BeanUtils.copyProperties(dto, insert);

            try {
                if (StringUtils.isNotBlank(dto.getStudentId())) {
                    TStudent tStudent = tStudentMapper.selectByPrimaryKey(dto.getStudentId());
                    insert.setHostName(tStudent.getNickName());
                    insert.setHostImgUrl(tStudent.getAvatarUrl());
                    insert.setShopId(null);
                } else if (StringUtils.isNotBlank(dto.getShopId())) {
                    TShop tShop = tShopMapper.selectByPrimaryKey(dto.getShopId());
                    insert.setHostName(tShop.getShopName());
                    insert.setHostImgUrl(tShop.getImgUrl());
                    insert.setStudentId(null);
                } else {
                    throw new RuntimeException();
                }
            } catch (Exception e) {
                return ResultUtil.error("未查询到发布人信息");
            }


            insert.setId(UUIDUtil.getUUID());
            if ("3".equals(insert.getType())) {
                insert.setAuthSuccess("1");
            } else {
                insert.setAuthSuccess("0");
            }


            insert.setCreateTime(new Date());
            insert.setUpdateTime(new Date());
            int res = tMessageMapper.insert(insert);
            if (res < 1) {
                return ResultUtil.error(SysCode.INSERT_ERROR);
            }
            return ResultUtil.success();
        } else {
            TMessage update = new TMessage();
            BeanUtils.copyProperties(dto, update);
            update.setUpdateTime(new Date());
            int res = tMessageMapper.updateByPrimaryKeySelective(update);
            if (res < 1) {
                return ResultUtil.error(SysCode.UPDATE_ERROR);
            }
        }
        return ResultUtil.success();
    }

    @Override
    @Transactional
    public ResultUtil delete(TMessageDto dto) {
        if (dto == null || StringUtils.isBlank(dto.getId())) {
            return ResultUtil.paramError();
        }
        int i = tMessageMapper.deleteByPrimaryKey(dto.getId());
        if (i > 0) {
            // 删除用户的收藏
            moduleTCollectionMapper.deleteByMsgId(dto.getId());
            return ResultUtil.success();
        }
        return ResultUtil.error(SysCode.DELETE_ERROR);
    }

    @Override
    public ResultUtil list(TMessageDto dto) {
        List<TMessageDto> list = moduleTMessageMapper.list(dto);
        return ResultUtil.success(list);
    }
}
